﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Invoicing.AJAX
{
    /// <summary>
    /// Userinfo 的摘要说明
    /// </summary>
    public class Userinfo : IHttpHandler
    {

        ZY.BLL.Tab_Userinfo dal = new ZY.BLL.Tab_Userinfo();
        private HttpRequest Req = HttpContext.Current.Request;


        public void ProcessRequest(HttpContext context)
        {

            if (Req["action"]!=null)
            {

                string action = Req["action"].ToString();
                switch (action)
                {
                    case "add":

                        #region 执行用户添加数据封装操作
                        try
                        {

                            ZY.Model.Tab_Userinfo model = new ZY.Model.Tab_Userinfo();
                            model.u_createtime = DateTime.Now;
                            model.u_lastime = DateTime.Now;
                            model.u_leve = int.Parse(Req["uleve"].ToString());
                            model.pid = int.Parse(Req["upostion"].ToString());//默认的职位
                            model.rid = int.Parse(Req["rid"].ToString());//默认的角色
                            model.dp_id = int.Parse(Req["upid"].ToString());
                            model.u_address = Req["txtaddres"].ToString();
                            model.u_email = Req["txtemail"].ToString();
                            model.u_mobilePhone = Req["txtmobile"].ToString();
                            model.u_note = Req["txtnote"].ToString();
                            model.u_password = Helper.DESEncrypt.Encrypt(Req["txtpassword"].ToString());
                            model.u_sn =0;//暂时默认，有需求了以后再调整
                            model.u_state = int.Parse(Req["ddlstate"].ToString());
                            model.u_trueName = Req["txtrueName"].ToString();
                            model.u_userName = Req["txtusername"].ToString();
                            model.u_power = Req["txtpower"].ToString();// 这里考虑要不要直接存入用户表
                            model.u_img = Req["txtimg"].ToString();//用户上传头像 我了个去，貌似有点难
                            dal.Add(model);
                            context.Response.Write(Pubs.AjaxHandle.AjaxPress("200", "添加成功!", "userlist", "sys/Userinfo.aspx"));

                        }
                        catch (Exception ex)
                        {
                            context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", ex.Message));

                        }
                        #endregion
                        break;
                    case "edit":

                        #region  执行用户编辑操作 
                        try
                        {
                            if (Req["uid"] != null)
                            {
                                ZY.Model.Tab_Userinfo upmodel = dal.GetModel(int.Parse(Req["uid"].ToString()));
                                upmodel.u_lastime = DateTime.Now;
                                upmodel.u_leve = int.Parse(Req["uleve"].ToString());
                                upmodel.pid = int.Parse(Req["upostion"].ToString());//默认的职位
                                upmodel.rid = int.Parse(Req["rid"].ToString());//默认的角色
                                upmodel.dp_id = int.Parse(Req["upid"].ToString());
                                upmodel.u_address = Req["txtaddres"].ToString();
                                upmodel.u_email = Req["txtemail"].ToString();
                                upmodel.u_mobilePhone = Req["txtmobile"].ToString();
                                upmodel.u_note = Req["txtnote"].ToString();
                               // upmodel.u_password = Helper.DESEncrypt.Encrypt(Req["txtpassword"].ToString());
                                upmodel.u_sn = 0;//暂时默认，有需求了以后再调整
                                upmodel.u_state = int.Parse(Req["ddlstate"].ToString());
                                upmodel.u_trueName = Req["txtrueName"].ToString();
                                upmodel.u_userName = Req["txtusername"].ToString();
                                upmodel.u_power = Req["txtpower"].ToString();// 这里考虑要不要直接存入用户表
                                upmodel.u_img = Req["txtimg"].ToString();//用户上传头像 我了个去，貌似有点难
                                dal.Update(upmodel);
                                context.Response.Write(Pubs.AjaxHandle.AjaxPress("200", "修改成功!", "userlist", "sys/Userinfo.aspx"));
                            }
                            else {

                                context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", "非法请求"));
                            }

                        }
                        catch (Exception ex)
                        {

                            Helper.LogHelper.Writelog(ex.Message);
                            context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", ex.Message));

                           
                        }

                        #endregion
                        break;

                    case "del":
                        #region 删除对象数据

                        string ids = Req["ids"].ToString();
                        string[] param = ids.Split(',');

                        foreach (var item in param)
                        {

                            try
                            {
                                if (item != null)
                                {
                                    dal.Delete(int.Parse(item));
                                }
                            }
                            catch (Exception ex)
                            {

                                context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", ex.Message));
                            }

                        }

                        context.Response.Write(Pubs.AjaxHandle.AjaxPressTodo("200", "删除成功"));


                        #endregion 



                        break;
                    default:
                        break;
                }




                
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}